const Sequelize = require("sequelize");
const common = require("../tool/common");
const db = require("./db");

let models = common.mapFiles("../api/model/");

models["Sequelize"] = Sequelize;
models["op"] = Sequelize.Op;

models["sys_ad"].hasMany(models["sys_ad_dtl"], { foreignKey: "ad_id", sourceKey: "id", as: "dtl" });

models["sys_ad_dtl"].belongsTo(models["sys_ad"], { foreignKey: "ad_id", targetKey: "id" });

models["sys_user"].belongsTo(models["sys_role"], { foreignKey: "roleId", targetKey: "id", as: "role" });

models["sys_form"].belongsTo(models["sys_model"], { foreignKey: "model_id", targetKey: "id", as: "model" });

models["sys_form"].belongsTo(models["sys_menu"], { foreignKey: "menu_id", targetKey: "id", as: "menu" });

models["sys_form_field"].belongsTo(models["sys_model_field"], { foreignKey: "model_field_id", targetKey: "id", as: "field" });

models.querySql = async (sql) => {
  return await db.sequelize.query(sql, { type: Sequelize.QueryTypes.SELECT });
};

module.exports = models;
